﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Indigo.Quibbler.Properties;

namespace Indigo.Quibbler.Controls
{
    #region Enum
    public enum SearchMode { Online, Local }
    #endregion


    /// <summary>
    /// Interaction logic for SettingsControl.xaml
    /// </summary>
    public partial class SettingsControl : UserControl
    {
        #region Properties
        public int ContentUpdateFrequency
        {
            get { return Settings.Default.ContentUpdateFrequency; }
            set { sliderFrequency.Value = Settings.Default.ContentUpdateFrequency = value; }
        }

        public int ContentDeleteFrequency
        {
            get { return Settings.Default.ContentDeleteFrequency; }
            set { sliderDays.Value = Settings.Default.ContentDeleteFrequency = value; }
        }

        public int ReadingSpeed
        {
            get { return Settings.Default.ReadingSpeed; }
            set { sliderReadingSpeed.Value = Settings.Default.ReadingSpeed = value; }
        }

        public string City
        {
            get { return Settings.Default.City; }
            set
            {
                Settings.Default.City = value;
                try
                {
                    if (!string.IsNullOrEmpty(value)) cbCity.SelectedItem = value;
                    else cbCity.SelectedIndex = 0;
                }
                catch { cbCity.SelectedIndex = 0; }
            }
        }

        public ViewType View
        {
            get { return Settings.Default.View; }
            set
            {
                Settings.Default.View = value;
                cbDefaultView.SelectedItem = value.ToString();
            }
        }

        public SearchMode SearchMode
        {
            get { return Settings.Default.SearchMode; }
            set
            {
                Settings.Default.SearchMode = value;
                cbSearchMode.SelectedItem = value.ToString();
            }
        }

        public string NewsPreference
        {
            get { return Settings.Default.NewsPrefernece; }
            set
            {
                Settings.Default.NewsPrefernece = value;
                cbNewsPreference.SelectedItem = value.ToString();
            }
        }

        public List<string> CityCollection
        {
            set
            {
                cbCity.Items.Clear();
                cbCity.Items.Add("None");
                foreach (string item in value)
                    cbCity.Items.Add(item);
                this.City = Settings.Default.City;
            }
        }
        #endregion

        #region Constructor
        public SettingsControl()
        {
            InitializeComponent();

            sliderFrequency.Value = this.ContentUpdateFrequency;
            sliderDays.Value = this.ContentDeleteFrequency;

            //cbDefaultView.Items.Add("select a view");
            string[] views = Enum.GetNames(typeof(ViewType));
            foreach (string view in views)
                if (view != ViewType.Carousel.ToString())
                    cbDefaultView.Items.Add(view);
            cbDefaultView.SelectedItem = Settings.Default.View.ToString();

            cbCity.Items.Add("choose your city");
            this.City = Settings.Default.City;

            string[] modes = Enum.GetNames(typeof(SearchMode));
            foreach (string mode in modes)
                cbSearchMode.Items.Add(mode);

            List<string> prefenceList = new List<string>() { "Latest Only (Best Performance)", "Show All" };
            foreach (string pref in prefenceList) cbNewsPreference.Items.Add(pref);
            //this.NewsPreference = Settings.Default.NewsPrefernece;

            Loaded += new RoutedEventHandler(SettingsControl_Loaded);
        }
        #endregion

        #region Events
        public event EventHandler SettingsChanged;
        private void _RaiseSettingsChanged()
        {
            if (SettingsChanged != null)
                SettingsChanged(this, new EventArgs());
        }
        #endregion

        #region Event Handlers
        void SettingsControl_Loaded(object sender, RoutedEventArgs e)
        {
            (this.Resources["sbHideSettings"] as Storyboard).Completed += new EventHandler(_HideCompleted);
            this.Visibility = Visibility.Collapsed;

            // Top Buttons
            btnCloseSettings.Click += new RoutedEventHandler(btnCloseSettings_Click);

            // Bottom Buttons
            btnSave.Click += new RoutedEventHandler(btnSaveSettings_Click);
            btnCancel.Click += new RoutedEventHandler(btnCloseSettings_Click);
            btnDefault.Click += new RoutedEventHandler(btnDefault_Click);
        }

        void btnDefault_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        void btnSaveSettings_Click(object sender, EventArgs e)
        {
            Save();
        }

        void btnCloseSettings_Click(object sender, EventArgs e)
        {
            Cancle();
        }

        void _HideCompleted(object sender, EventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region Helper Functions
        private void Save()
        {
            // Reset to default settings:
            this.ContentUpdateFrequency = (int)sliderFrequency.Value;
            this.ContentDeleteFrequency = (int)sliderDays.Value;
            this.ReadingSpeed = (int)sliderReadingSpeed.Value;
            this.City = (cbCity.SelectedIndex == 0) ? string.Empty : (string)cbCity.SelectedItem;
            this.View = (ViewType)Enum.Parse(typeof(ViewType), (string)cbDefaultView.SelectedItem);
            this.SearchMode = (SearchMode)Enum.Parse(typeof(SearchMode), (string)cbSearchMode.SelectedItem);
            this.NewsPreference = (string)cbNewsPreference.SelectedItem;
            this.Hide();

            try { Settings.Default.Save(); }
            catch
            {
                MessageBox.Show("Error in saving settings.", "Save error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            _RaiseSettingsChanged();
        }

        public void Reset()
        {
            // Reset to default settings:
            sliderFrequency.Value = Settings.Default.DefaultContentUpdateFrequency;
            sliderDays.Value = Settings.Default.DefaultContentDeleteFrequency;
            sliderReadingSpeed.Value = Settings.Default.DefaultReadingSpeed;
            cbDefaultView.SelectedItem = Settings.Default.DefaultView.ToString();
            cbNewsPreference.SelectedItem = Settings.Default.DefaultNewsPrefernece.ToString();
            if (!string.IsNullOrEmpty(Settings.Default.DefaultCity))
                cbCity.SelectedItem = Settings.Default.DefaultCity.ToString();
            else
                cbCity.SelectedIndex = 0;
        }

        private void Cancle()
        {
            this.Hide();
        }
        #endregion

        #region Public Helpers
        public void Show()
        {
            this.ContentUpdateFrequency = Settings.Default.ContentUpdateFrequency;
            this.ContentDeleteFrequency = Settings.Default.ContentDeleteFrequency;
            this.ReadingSpeed = Settings.Default.ReadingSpeed;
            this.City = Settings.Default.City;
            this.View = Settings.Default.View;
            this.SearchMode = Settings.Default.SearchMode;
            this.NewsPreference = Settings.Default.NewsPrefernece;
            this.Visibility = Visibility.Visible;

            (this.Resources["sbShowSettings"] as Storyboard).Begin();
        }
        public void Hide()
        {
            (this.Resources["sbHideSettings"] as Storyboard).Begin();
        }
        #endregion
    }

    public class ReadSpeedTextConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double numValue = 0;
            double.TryParse(value.ToString(), out numValue);
            if (numValue < 0)
                return "slow";
            else if (numValue > 0)
                return "fast";
            else
                return "medium";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
